#include "VerticesConstructor.h"
#include "QD3D\QD3DUtils.h"

VerticesConstructor::VerticesConstructor ()
{
	_pVerticesConstructDxCS = NULL;
}

VerticesConstructor::~VerticesConstructor ()
{
}

void VerticesConstructor::initialize ()
{
	QD3DUtils d3DUtils;

	QD3DShaderDesc shaderDesc;
	shaderDesc.setDefine ("FACE_THRESH", FACE_THRESH_STR);
	shaderDesc.setDefine ("REGION_SIZE", RECONSTRUCT_REGION_SIZE_STR);
	shaderDesc.setDefine ("FW", FW_STR);
	shaderDesc.setDefine ("FH", FH_STR);
	_pVerticesConstructDxCS =
			d3DUtils.createShaderFromFile< ID3D11ComputeShader > (
			QD3DUtils::COMPUTE_SHADER, "cs_5_0",
			"..\\Data\\Shaders\\VerticesConstructCS.hlsl", "csMain", shaderDesc);
}

void VerticesConstructor::clean ()
{
	SAFE_RELEASE (_pVerticesConstructDxCS);
}

void VerticesConstructor::process (std::vector< SensorResource > &sensorReses)
{
	QD3DUtils d3DUtils;

	QVector< ID3D11Buffer * > dxConstBufPtrs (3);
	QVector< ID3D11ShaderResourceView * > dxSRVPtrs(2);
	QVector< ID3D11UnorderedAccessView * > dxUAVPtrs(1);

	d3DUtils.setComputeShader (_pVerticesConstructDxCS);
	for (size_t i = 0; i < sensorReses.size (); ++i) {
		dxConstBufPtrs[0] = sensorReses[i].p_sensorIntDxConstBuf;
		dxConstBufPtrs[1] = sensorReses[i].p_sensorExtDxConstBuf;
		dxConstBufPtrs[2] = sensorReses[i].p_verticesOffsetDxConstBuf;
		d3DUtils.setConstBufs (QD3DUtils::COMPUTE_SHADER, 0, dxConstBufPtrs);
		dxSRVPtrs[0] = sensorReses[i].pRgbDxSRV;
		dxSRVPtrs[1] = sensorReses[i].pDepthDxSRV;
		d3DUtils.setSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs);
		dxUAVPtrs[0] = sensorReses[i].rp_verticesPack->getUAV ();
		d3DUtils.setUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs);
		d3DUtils.dispatch (FRAME_HEIGHT, 1, 1);
	}
	d3DUtils.clearConstBufs (QD3DUtils::COMPUTE_SHADER, 0, dxConstBufPtrs.size ());
	d3DUtils.clearSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs.size ());
	d3DUtils.clearUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs.size ());
}
